package com.atguigu.tingshu.payment.service.impl;

import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WxPayServiceImpl implements WxPayService {

    @Autowired
    private PaymentInfoService paymentInfoService;
    @Autowired
    private RSAAutoCertificateConfig rsaAutoCertificateConfig;
    @Autowired
    private WxPayV3Config wxPayV3Config;

    /**
     * 支付方式为微信支付时,创建小程序拉起微信接口所需参数map
     *
     * @param paymentType 支付类型: 1301订单 1302余额充值
     * @param orderNo
     * @return {"timeStamp":"","package":"","paySign":"","signType":"RSA","nonceStr":""}
     */
    @Override
    public Map<String, String> createJsapi(String paymentType, String orderNo) {
        //获取本地交易记录:订单支付状态
        PaymentInfo paymentInfo = paymentInfoService.savePaymentInfo(paymentType, orderNo);
        String paymentStatus = paymentInfo.getPaymentStatus();
        //判断订单支付状态,未支付订单方可调用微信支付
        if (paymentStatus.equals(SystemConstant.PAYMENT_STATUS_PAID)) {
            throw new RuntimeException("订单已支付");
        }

        //对接微信支付接口,获取拉起微信支付所需参数
        //创建微信支付业务对象JSAPI
        JsapiServiceExtension jsapiServiceExtension = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig).build();
        //创建预下单请求对象
        PrepayRequest prepayRequest = new PrepayRequest();
        //封装请求体参数
        Amount amount = new Amount();
        amount.setTotal(1);     //测试用数据,1分
        prepayRequest.setAmount(amount);
        prepayRequest.setDescription(paymentInfo.getContent());
        prepayRequest.setAppid(wxPayV3Config.getAppid());
        prepayRequest.setMchid(wxPayV3Config.getMerchantId());
        prepayRequest.setNotifyUrl(wxPayV3Config.getNotifyUrl());
        //封装订单号
        prepayRequest.setOutTradeNo(orderNo);
        //封装支付者信息
        Payer payer = new Payer();
        payer.setOpenid("odo3j4qp-wC3HVq9Z_D9C0cOr0Zs");
        prepayRequest.setPayer(payer);

        //获取并封装响应结果返回前端
        PrepayWithRequestPaymentResponse response = jsapiServiceExtension.prepayWithRequestPayment(prepayRequest);
        if (response != null) {
            String timeStamp = response.getTimeStamp();
            String packageVal = response.getPackageVal();
            String paySign = response.getPaySign();
            String signType = response.getSignType();
            String nonceStr = response.getNonceStr();
            HashMap<String, String> map = new HashMap<>();
            map.put("timeStamp", timeStamp);
            map.put("package", packageVal);
            map.put("paySign", paySign);
            map.put("signType", signType);
            map.put("nonceStr", nonceStr);
            return map;
        }
        return null;
    }

    /**
     * 支付状态查询
     *
     * @param orderNo
     * @return
     */
    @Override
    public Boolean queryPayStatus(String orderNo) {
        //模拟微信交易
        Transaction transaction = new Transaction();
        transaction.setOutTradeNo(orderNo);
        transaction.setTransactionId("wx"+ IdUtil.getSnowflakeNextId());
        //处理核心业务
        paymentInfoService.updatePaymentInfo(transaction);
        return true;
    }
}
